package com.eastfair.venuebooking.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venuebooking.dto.SalesAnalysisDTO;
import com.eastfair.venuebooking.dto.SpacePageQuery;
import com.eastfair.venuebooking.dto.SpaceSaveDTO;
import com.eastfair.venuebooking.dto.SpaceUpdateDTO;
import com.eastfair.venuebooking.entity.Space;
import com.eastfair.venuebooking.service.SpaceService;
import com.eastfair.venuebooking.vo.SpaceVO;
import com.eastfair.venueservice.vo.ExhibitionManageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 场地表
 * </p>
 *
 * @author linan
 * @date 2022-06-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/space")
@Api(value = "Space", tags = "场地表")
@PreAuth(replace = "venuebooking:space:")
public class SpaceController extends SuperController<SpaceService, Long, Space, SpacePageQuery, SpaceSaveDTO, SpaceUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private SpaceService spaceService;


    @Override
    public void handlerResult(IPage<Space> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<Space> spaceList = list.stream().map((map) -> {
            Space space = Space.builder().build();
            //TODO 请在这里完成转换
            return space;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(spaceList));
    }


    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<SpacePageQuery> params) {
        SpacePageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }

    }


    /**
     * 场地展厅列表
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "查询展厅列表")
    @PostMapping(value = "/querySpaceList")
    public R<IPage<SpaceVO>> querySpaceList(@RequestBody PageParams<SpacePageQuery> params) {
        IPage<SpaceVO> page = spaceService.querySpaceList(params);
        return R.success(page);
    }

    /**
     * 展厅名称查询展厅.
     *
     * @param spaceName the space name
     * @return the by space name
     */
    @GetMapping("/get/spaceName/{spaceName}")
    public R<SpaceVO> getBySpaceName(@PathVariable("spaceName") String spaceName) {
        log.info("getBySpaceName - 展厅名称查询展厅, spaceName={}", spaceName);
        try {
            return R.successDef(spaceService.getBySpaceName(spaceName));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 添加展厅信息
     *
     * @return}
     */
    @ApiOperation(value = "添加展厅信息")
    @SysLog("添加展厅")
    @PostMapping(value = "/saveSpaceInfo")
    public R<Boolean> saveSpaceInfo(@RequestBody @Validated(SpaceSaveDTO.Save.class) SpaceSaveDTO spaceSaveDTO){

        return R.success(spaceService.saveSpaceInfo(spaceSaveDTO));
    }


    /**
     * 修改展厅信息
     *
     * @return
     */
    @ApiOperation(value = "修改展厅信息")
    @SysLog("修改展厅")
    @PostMapping(value = "/updateSpaceInfo")
    public R<Boolean> updateSpaceInfo(@RequestBody @Validated(SpaceUpdateDTO.Update.class) SpaceUpdateDTO spaceUpdateDTO){

        return R.success(spaceService.updateSpaceInfo(spaceUpdateDTO));
    }

    /**
     * 逻辑删除展厅信息
     *
     * @return
     */
    @ApiOperation(value = "逻辑删除展厅信息")
    @SysLog("删除展厅")
    @PostMapping(value = "/delSpaceInfoById")
    public R<Boolean> delSpaceInfoById(@RequestBody SpaceUpdateDTO SpaceUpdateDTO) {
        boolean b = spaceService.delSpaceInfoById(SpaceUpdateDTO);
        return R.success(b);
    }


    /**
     * 获取展厅详情
     *
     * @param id
     * @return
     */
    @GetMapping("/getSpaceById/{id}")
    public R<SpaceVO> getSpaceById(@PathVariable Long id) {
        SpaceVO spaceVO = spaceService.getSpaceById(id);
        return R.success(spaceVO);
    }

    /**
     * 查询所有标签.
     *
     * @return the r
     */
    @Deprecated
    @GetMapping("/tags")
    public R<List<String>> queryTags() {
        log.info("queryTags - 查询所有标签");
        return R.successDef(spaceService.listTags());
    }


    /**
     * 展会同步回调
     *
     * @return R
     */
    @ApiOperation(value = "展会同步回调", notes = "展会同步回调")
    @PostMapping("/syncCallback")
    public R<Boolean> syncCallback(@RequestBody List<Long> ids) {
        log.info("syncCallback - 展会同步回调");
        return R.success(baseService.syncCallback(ids));
    }




    /**
     * 查询未同步的所有展厅
     *
     * @return R
     */
    @ApiOperation(value = "查询未同步的所有展厅", notes = "查询未同步的所有展厅")
    @PostMapping("/queryNeedSyncSpace")
    public R<List<SpaceVO>> queryNeedSyncSpace() {
        log.info("queryNeedSyncSpace - 查询未同步的所有展厅");
        return R.success(baseService.queryNeedSyncSpace());
    }


    /**
     * 获取场地展厅列表不分页
     *
     * @param salesAnalysisDTO
     * @return
     */
    @ApiOperation(value = "获取场地展厅列表不分页")
    @PostMapping(value = "/getSpaceList")
    public R<List<Space>> getSpaceList(@RequestBody SalesAnalysisDTO salesAnalysisDTO) {
        List<Space> spaceList = spaceService.getSpaceList(salesAnalysisDTO);
        return R.success(spaceList);
    }

    /**
     * 导入展厅.
     *
     * @param file the file
     * @return the r
     * @throws IOException the io exception
     */
    @PostMapping(value = "/import/space")
    public R<Boolean> importSpace(@RequestBody MultipartFile file) throws IOException {
        log.info("importSpace - 导入展厅");
        return R.successDef(spaceService.importSpace(file));
    }

    /**
     * 获取展厅导入模版.
     *
     * @param response the response
     */
    @GetMapping("/import/template")
    public void getImportTemplate(HttpServletResponse response) throws Exception {
        log.info("getImportTemplate - 获取展厅导入模版");
        spaceService.getImportTemplate(response);
    }
}
